

// import store from '@/store'
export default {
    data() {
        return {
            innerList: [],
            remoteObj: {},
            dataLoading: false,
            componentLoading: false,
            warnning: null,
        }
    },
    props: {
        items: {
            type: [Array, Object],
            default: () => [],
        },
        cache: {
            type: Boolean,
            default: false,
        },

        listHandler: {
            type: Function,
            default: null
        },
        enum: String,

        url: String,
        urlType: {
            type: String,
            default: 'form',
        },
        urlParams: {
            type: [Object, Function],
            default: () => {
                return {}
            }
        },
        urlControl: {
            type: Function,
            default: null
        },
        dependentId: {
            type: String,
            default: null
        },
        option: [Object, Function],
        storeData: Boolean,

        i18n: Boolean,
    },
    mounted() {
        if (this.url) {
            this.remoteObj = { componentLoading: true }
            this._initRemoteList()
        } else if (this.enum) {
            this._initEnumList()
        }
    },
    computed: {

        requestParamsQuery: function () {
            return this.$queryStr(this.requestParams())
        },
        // requestKey: function () {
        //     let key = this.url
        //     if (!_.isEmpty(this.requestParams())) {
        //         key = key + this.requestParamsQuery
        //     }
        //     return key
        // },
        _storeList() {
            // return store.state.commonMapCache[this.url] ? store.state.commonMapCache[this.url][this.requestParamsQuery] : null
            return this.$store.state.commonMapCache[this.url] ? this.$store.state.commonMapCache[this.url][this.requestParamsQuery] : null
        },
        _useList: function () {
            if (this.storeData) return this._storeList
            if (_.isEmpty(this.innerList)) return this.items

            return this.innerList
        },
        _actualList: function () {
            let _list = this._useList
            if (_list) {
                let _isObj = _list.some(it => it instanceof Object)
                if (this.listHandler) {
                    _list = this.listHandler(_list,)
                }
                if (_isObj) {
                    if (this.option instanceof Function) {
                        _list = _list.map(it => {
                            let { label: _label, value: _value } = this.option(it)
                            return Object.assign({}, it, { _label, _value })
                        })
                    } else {
                        let first = _.head(_list)
                        let _label_props = (this.option && this.option.label) || (first._label && '_label') || (first.label && 'label')
                        let _value_props = (this.option && this.option.value) || (first._value && '_value') || (first.value && 'value')
                        _list = _list.map(it => {
                            let _label = _label_props instanceof Function ? _label_props(it) : it[_label_props]
                            let _value = it[_value_props] ? it[_value_props] : it
                            _label = this.i18n ? this.$tt(_label) : _label
                            return Object.assign({}, it, { _label, _value })
                        })
                    }
                } else {
                    _list = _list.map(it => {
                        return {
                            _label: this.enum ? this.$enumShow(it, this.enum) : this.i18n ? this.$tt(it) : it,
                            _value: it
                        }
                    })
                }
            }
            return _list ? _list : []
        },
    },

    watch: {
        _actualList(newV, oldV) {
            this.$emit('initList', this._useList)
        },
        dependentId(newV, oldV) {
            this._getRemoteData()
        },
    },
    methods: {
        requestParams() {
            if (this.urlParams instanceof Function) {
                return this.urlParams()
            }
            return this.urlParams
        },
        _emitValue(value, list) {
            if (!list) list = ['input']
            list.forEach(key => {
                this.$emit(key, value)
            })

        },

        reloadData(config) {
            if (!config) config = {}
            this.remoteObj = { componentLoading: !!config.componentLoading, dataLoading: true }
            this._initRemoteList()
            if (_.keys(config).some(it => it == 'initValue')) {
                this._emitValue(config.initValue)
            }
        },

        _initEnumList() {
            let _list = this.$localEnum[this.enum]
            if (!_list) _list = this.$enumOptions[this.enum]
            if (_list) {
                this.innerList = _list
            } else {
                console.error(this.enum + " is undefined")
            }

        },
        _initRemoteList(remoteConfig) {
            if (this.storeData && this._storeList) {

            } else {
                this._getRemoteData(remoteConfig)
            }
        },

        _getRemoteData(remoteConfig) {
            if (!remoteConfig) remoteConfig = {}

            let isSetIntoData = !this.storeData || remoteConfig.setIntoData

            let params = this.requestParams()
            if (remoteConfig.remoteParams) {
                params = Object.assign({}, params, this.remoteObj.params,)
            }
            if (this.urlControl) {
                let control = this.urlControl(params)

                if (control instanceof Object) {
                    if (control.netStop) {
                        if (control.focusInnerData) this.innerList = control.focusInnerData
                        if (control.warnning) this.warnning = control.warnning
                        return
                    }
                }
            }
            let _hasStackNet = true
            let _netStack = []
            let _netCallback = {
                success: (res) => {
                    this.warnning = null
                    if (res.data && res.data.rows && isSetIntoData) {
                        this.innerList = res.data.rows
                    }
                },
                failure: () => {
                    this.warnning = this.$tt('tip_data_load_fail')
                },
                complete: () => {
                    this.componentLoading = false
                    this.dataLoading = false
                }
            }
            if (this.$NetCacheStack) {
                _netStack = this.$NetCacheStack.get(this.url, params)
                if (_netStack && _netStack.length > 0) {
                    _hasStackNet = false
                }
            }
            _netStack.push(_netCallback)
            this.innerList = []
            this.componentLoading = !!this.remoteObj.componentLoading
            this.dataLoading = !!this.remoteObj.dataLoading
            if (_hasStackNet) {

                this.$ajaxCache({
                    url: this.url,
                    method: 'post',
                    type: this.urlType,
                    data:params,
                    cache: this.cache,
                    tip: false,
                    success: (res) => {
                        if (res.data && res.data.rows && !isSetIntoData) {
                            this.$store.commit("pushCommonData", {
                                group: this.url,
                                key: this.requestParamsQuery,
                                data: res.data.rows
                            })
                        }
                        _netStack.forEach(it => it.success(res));
                    },
                    failure: () => {
                        _netStack.forEach(it => it.failure());
                    },
                    complete: () => {
                        _netStack.forEach(it => it.complete());
                        if (this.$NetCacheStack) {
                            this.$NetCacheStack.delete(this.url, params)
                        }
                    }
                })
            }
        }
    }
}